//
//  SettingViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/22.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit

class SettingViewController: UIViewController {
     var tableView : UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.title = "设置"
        
        tableView = UITableView(frame:self.view.frame, style:.grouped)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
 
        tableView.tableFooterView = UIView.init()
        
        view.addSubview(tableView)
        tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        // Do any additional setup after loading the view.
    }
    



}
extension SettingViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 40
        
    }
 
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            
            
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            if cell == nil {
                
                cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
            }
            cell?.textLabel?.text = "修改密码"
            cell?.textLabel?.font = UIFont.systemFont(ofSize:12)
            cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
            return cell!
            
            
        }else{
            
            
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell1")
            if cell == nil {
                
                cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell1")
            }
            cell?.textLabel?.text = "退出登录"
            cell?.textLabel?.font = UIFont.systemFont(ofSize:12)
            cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
            return cell!
        }
        
        
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
       
        
        if indexPath.section == 0 {
             print("=====\(indexPath.section)")
            
        }else{
            
            let alertController = UIAlertController(title: "系统提示",
                                                    message: "您确定要退出", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let okAction = UIAlertAction(title: "好的", style: .default, handler: {
                action in
                print("点击了确定")
               
                UserDefaults.standard.removeObject(forKey: "tokens")
                 UserDefaults.standard.removeObject(forKey: "accounts")
                 UserDefaults.standard.removeObject(forKey: "names")
                
                let login =   LoginViewController()
                self.navigationController?.pushViewController(login, animated: true)
                
            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
            
        }
        
        
    }
    
}
